// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const managertoolCollection = db.collection('manager_tool')
let managerData = {}
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const caller = event.caller
  
  await managertoolCollection.doc('d7cac9a4-2649-4d3b-8fcf-7eaae2902dcd').get().then(res => {
    managerData = res.data
  })

  if (caller == 'manager') {
    let state1 = (new Date().getTime() - managerData.homeTextCreateDate) > Number(managerData.homeConstantTime) * 24 * 3600 * 1000
    if(state1){
      await managertoolCollection.doc('d7cac9a4-2649-4d3b-8fcf-7eaae2902dcd').update({
        data: {
          homeState: false
        }
      }).then(res => {
        managerData.homeState = false
      })
    }

    let state2 = (new Date().getTime() - managerData.userTextCreateDate) > Number(managerData.userConstantTime) * 24 * 3600 * 1000
    if (state2) {
      await managertoolCollection.doc('d7cac9a4-2649-4d3b-8fcf-7eaae2902dcd').update({
        data: {
          userState: false
        }
      }).then(res => {
        managerData.userState = false
      })
    }

    let state3 = (new Date().getTime() - managerData.recommendCreateDate) > Number(managerData.recommendConstantTime) * 24 * 3600 * 1000
    if (state3) {
      await managertoolCollection.doc('d7cac9a4-2649-4d3b-8fcf-7eaae2902dcd').update({
        data: {
          recommendState: false
        }
      }).then(res => {
        managerData.recommendState = false
      })
    }
  }
  return managerData
}